﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Management;
using io = System.IO;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Threading;
using System.Text.RegularExpressions;
using System.Net;
using System.Net.NetworkInformation;
using Forms = System.Windows.Forms;
using Draw = System.Drawing;


namespace MOJsdk
{
    public class TOOLS
    {
        public static string TMDB_APIKey
        { get { return "6b3254ee1455766f9901e9ec383670d8"; } }

        /// <summary>
        /// Get DriveID
        /// </summary>
        /// <param name="PATH"></param>
        /// <returns></returns>
        public static string GetDriveID(string PATH)
        {
            return GetDriveID(PATH[0]);
        }
        public static string GetDriveID(char DriveLetter)
        {
            try
            {
                ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + DriveLetter + ":\"");
                disk.Get();

                string p = disk["VolumeSerialNumber"].ToString();

                if (String.IsNullOrEmpty(p))
                    return null;

                return p;
            }
            catch { return null; }
        }
        /// <summary>
        /// Get Drive Letter by ID
        /// </summary>
        /// <param name="DriveID">string with hardware id</param>
        /// <returns>null if unvailble</returns>
        public static string GetDriveLetter(string DriveID)
        {
            string letter = null;
            int i;
            for (i = 65; i < 91; i++)
            {
                if (DriveID == GetDriveID(char.ConvertFromUtf32(i)))
                {
                    letter = char.ConvertFromUtf32(i);
                    break;
                }
            }

            if (i == 91) return null;

            return letter;
        }

        public static int DamerauLevenshteinDistance(string source, string target)
        {
            if (String.IsNullOrEmpty(source))
            {
                if (String.IsNullOrEmpty(target))
                {
                    return 0;
                }
                else
                {
                    return target.Length;
                }
            }
            else if (String.IsNullOrEmpty(target))
            {
                return source.Length;
            }

            var score = new int[source.Length + 2, target.Length + 2];

            var INF = source.Length + target.Length;
            score[0, 0] = INF;
            for (var i = 0; i <= source.Length; i++) { score[i + 1, 1] = i; score[i + 1, 0] = INF; }
            for (var j = 0; j <= target.Length; j++) { score[1, j + 1] = j; score[0, j + 1] = INF; }

            var sd = new SortedDictionary<char, int>();
            foreach (var letter in (source + target))
            {
                if (!sd.ContainsKey(letter))
                    sd.Add(letter, 0);
            }

            for (var i = 1; i <= source.Length; i++)
            {
                var DB = 0;
                for (var j = 1; j <= target.Length; j++)
                {
                    var i1 = sd[target[j - 1]];
                    var j1 = DB;

                    if (source[i - 1] == target[j - 1])
                    {
                        score[i + 1, j + 1] = score[i, j];
                        DB = j;
                    }
                    else
                    {
                        score[i + 1, j + 1] = Math.Min(score[i, j], Math.Min(score[i + 1, j], score[i, j + 1])) + 1;
                    }

                    score[i + 1, j + 1] = Math.Min(score[i + 1, j + 1], score[i1, j1] + (i - i1 - 1) + 1 + (j - j1 - 1));
                }

                sd[source[i - 1]] = i;
            }

            return score[source.Length + 1, target.Length + 1];
        }
        public static string RemoveIlegalChars(string input)
        {
            input = input.Replace("\"", "");
            return Regex.Replace(input, @"[\/:*?<>|]", "");
        }
        public static IPAddress GetLocalIP()
        {
            
            IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
                if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    return ip;

            return null;
        }
        public static Point GetCursorPos()
        {
            Draw.Point pt = Forms.Cursor.Position;
            return new Point(pt.X, pt.Y);
        }

        #region WIN32
        [DllImport("User32.dll")]
        private static extern bool SetCursorPos(int X, int Y);
        public static bool SetMousePos(Point p)
        {
            return SetCursorPos((int)p.X, (int)p.Y);
        }

        [DllImport("User32.dll")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
        public static void LeftMouseClick()
        {
            Point p = GetCursorPos();
            mouse_event(0x02, (int)p.X, (int)p.Y, 0, 0);
            mouse_event(0x04, (int)p.X, (int)p.Y, 0, 0);
        }
        public static void RightMouseClick()
        {
            Point p = GetCursorPos();
            mouse_event(0x08, (int)p.X, (int)p.Y, 0, 0);
            mouse_event(0x10, (int)p.X, (int)p.Y, 0, 0);
        }
        #endregion
    }
}
